/**
 * 
 */
package controller.xml;

import model.preprocessor.Satellite_1_0Parameter;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

/**
 * Use to parse a {@link Satellite_1_0Parameter} in the exported session in XML.
 * 
 * @author GRONOWSKI Stephane svan001@aol.com
 * 
 *         igsat 2010
 * 
 */
public class SatelliteParameterXMLHandler extends XMLHandler {
	private Satellite_1_0Parameter		param	= null;
	private SatelliteParameterXMLStatus	status	= SatelliteParameterXMLStatus.NONE;

	// private SolverParameterXMLHandler paramHandler = null;


	public SatelliteParameterXMLHandler() {
	}


	@Override
	public boolean characters2(char[] ch, int start, int length) throws SAXException {

		switch (status) {
		case NONE:
		case PREPROC_PARAMETER:
			throw new SAXException("not supposed to receive character in this status");
		default:
			return super.characters2(ch, start, length);
		}
	}

	@Override
	public boolean endDocument2() throws SAXException {
		return false;
	}

	@Override
	public boolean endElement2(String uri, String localName, String qName) throws SAXException {
		switch (status) {
		case NONE:
			return false;

		case PREPROC_PARAMETER:
			if (qName.equals(XMLBaliseFormat.PREPROC_PARAMETER))
			{// ferme basic param
				status = SatelliteParameterXMLStatus.NONE;
				param = new Satellite_1_0Parameter(((PreProcessorParameterXMLHandler) delegate).getParam());
				delegate = null;
				return true;
			}
			throw new SAXException("end of PREPROC PARAMETER was expected");
		}

		// attend une fin de balise qui existe pas...
		throw new SAXException("an other end of element was expected");
	}

	@Override
	public boolean startElement2(String arg0, String arg1, String arg2, Attributes arg3) throws SAXException {
		if (status != SatelliteParameterXMLStatus.NONE)
			throw new SAXException("already have a started element");

		if (arg2.equals(XMLBaliseFormat.PREPROC_PARAMETER))
		{// balise parameter
			if (param != null)
				throw new SAXException("already have a SOLVER PARAMETER");

			// set les donnees pour recupere le basic result
			status = SatelliteParameterXMLStatus.PREPROC_PARAMETER;
			delegate = new PreProcessorParameterXMLHandler();

			return true;
		}

		// si pas de param refuse les autres
		if (param == null)
			throw new SAXException("need a PREPROC PARAMETER	 first");


		// peut pas commencer balise qui existe pas
		throw new SAXException("unexpected balise: " + arg2);
	}

	/**
	 * Return the {@link Satellite_1_0Parameter}.
	 * 
	 * @return the param
	 */
	public Satellite_1_0Parameter getParam() {
		return param;
	}
}
